﻿//共享资源URL
var GroupURL = parent.location.toString();
//选中的朋友号
var CFNList = new Array();
//当前页朋友号列表()
var FriendNoList = new Array();
//当前会员好友数量
var MCOUNT = 0;
//好友页索引
var PageIndex = 1;
//全选后取消单个标记(true全选后取消单个)
var CheckAOrF = false;
//我的昵称名
var MyNickN = "";
var MyMemberNo = "";
//分页对象
var FriendPage = null;
var gPageIndex = 1;
var PageSize = 35;
var ivC0047 = "";
var ivC0056 = "";
var ivC0064 = "";
var ivSR000 = "";
var ivSR001 = "";
var ivSR002 = "";
var ivSR003 = "";
var ivPP030 = "";
var ivSR009 = "";
var ivSR010 = "";
var ivSR014 = "";
var ivSR017 = "";
var ivSR018 = "";
var ivGroupTitle = "";
//邀请成员（group标题）
function Invite(title)
{
    ivGroupTitle = title;
    showProgressBar(false,true);
    WebServices.WWW.Groups.GetInviteInfo(1,PageSize,onsuccInviteData);
}
function onsuccInviteData(result)
{
    showProgressBar(false,true);    
    if(result[0]=="NL")
    {
        $Login$(Invite);return;
    }
    else if(result[0]=="F")
    {alert("F");inviteEmpty();return;
        //显示操作失败消息
    }
    else
    {//0：当前会员好友数量、1：朋友信息（号、昵称）、2：当前会员昵称名、3:文字相关、4：
        MCOUNT = result[0];MyNickN = result[2];        
        var friLength = result[1].length;
        ivC0047 = result[3][0];
        ivC0056 = result[3][1];
        ivC0064 = result[3][2];
        ivSR000 = result[3][3];
        ivSR001 = result[3][4];
        ivSR002 = result[3][5];
        ivSR003 = result[3][6];
        ivPP030 = result[3][7];
        //ivSR006 = result[3][8];
        ivSR009 = result[3][8];
        ivSR010 = result[3][9];
        ivSR014 = result[3][10];
        ivSR017 = result[3][11];
        ivSR018 = result[3][12];
        var _title = result[3][13];FriendPage = result[4];MyMemberNo = result[5];
        $("$divIput$").style.display = "none";
        var srBody = "<div><div id='SendContent' style='display:none'><div class='popup_content' style='text-align:center'><strong>"+ivSR017+"</strong><div id='iv_SendText'></div><input type='button' value='"+ivSR018+"' class='button' onclick='SendContents()' />&nbsp;&nbsp;<input type='button' value='"+ivC0056+"' class='buttong' onclick='inviteEmpty()' /></div></div><div id='Send'><div class='popup_content'><div><strong>"+ivPP030+":</strong><br /><input type='text' value='"+GroupURL+"' class='common460' disabled='disabled' readonly='readOnly' /><div class='clear'></div></div><div class='margin20T'><ul class='sort_sel' style='width:540px'><li id='spanQuesType1' class='first selATags' onclick='ShowType(0)' style=' float:left'><span>"+ivSR000+"</span></li><li id='spanQuesType2' class='' onclick='ShowType(1)' style='float:left'><span>"+ivSR001+"</span></li></ul><div class='clear'></div></div><div id='askQuestype1'><div style='margin-top:10px'><div id='iv_emOthErr' class='MessagePanel4' style='display:none'>"+ivSR003+"</div>"+ivSR002+"<p><textarea id='iv_txtEmail' class='common460' rows='5'></textarea></p><input type='button' value='"+ivC0047+"' class='button' onclick='SendEmail()' />&nbsp;&nbsp;<input type='button' value='"+ivC0056+"' class='buttong' onclick='inviteEmpty()' /></div></div><div id='askQuestype2' style='display:none'><div id='iv_SelectFriends' ><div style='border-bottom:1px #cccccc solid; padding-bottom:3px; margin-top:10px; margin-bottom:10px'><div style='float:left'></div><div style='float:right'><input type='checkbox' id='iv_checkDel' title='"+ivSR009+" / "+ivC0056+"' onclick='CheckAllBox(this)' /><font color='#ff0000' size='1'>"+ivSR009+"</font></div><div class='clear'></div></div><div id='iv_error1ID' style='display:none'><div class='MessagePanel4'><div style='margin-left:10px'>"+ivSR014+"</div></div></div><div id='iv_FriendsList'><ul class='tag_common_right'>";
        for(var i=0; i < friLength; i++)
        {
            FriendNoList[i] = result[1][i].MemberNo;
            srBody+="<li><input type='checkbox' id='"+result[1][i].MemberNo+"' value='"+result[1][i].Nickname+"' onclick='CheckFriend(\""+result[1][i].MemberNo+"\",this)' /> <a href='/"+result[1][i].MemberNo+".htm' target='_blank'>"+result[1][i].Nickname+"</a></li>";
        }
        srBody+="</ul><div class='clear'></div></div><div class='clear'></div> <div id='iv_divPage' class='DisplayNone'></div><div class='margin5'><input type='button' value='"+ivC0047+"' class='button' onclick='SendItalkiMail()'/>&nbsp;&nbsp;<input type='button' value='"+ivC0056+"' class='buttong' onclick='inviteEmpty()' /></div></div><div id='iv_NoFriends' style='display:none'><h3>"+ivSR010+"</h3></div></div></div></div><div id='iv_FriendsTemple' style='display:none'><ul class='tag_common_right'><li><input type='checkbox' id='$MemberNo$' onclick='CheckFriend(\"$MemberNo$\",this)' /> <a href='/$MemberNo$.htm' target='_blank'>$NickName$</a></li></ul></div>";
        var obj = {}
        $ShowMessageBox$(_title,srBody,obj,inviteEmpty);
    }
}

//显示发送内容1：邮件、2：昵称
function SendContents()
{
    $get("SendContent").value="";$get("SendContent").style.display="none";$get("Send").style.display="";
}
//显示（0:Send an Email，1:italki friends）
function ShowType(val)
{  
    if(val==0){        
        $get("askQuestype1").style.display="";$get("askQuestype2").style.display="none";$get("spanQuesType1").className="first selATags";$get("spanQuesType2").className="unselATags";
    }
    else if(val==1){
        if(FriendPage.RecordCount==0)
        {
            $get("iv_NoFriends").style.display="";
            $get("iv_SelectFriends").style.display="none";
        }else{
            RendPageNavigator(FriendPage,"iv_divPage","GetPageFriend");
        }
        $get("askQuestype1").style.display="none";$get("askQuestype2").style.display="";$get("spanQuesType1").className="first unselATags";$get("spanQuesType2").className="selATags";
    }
}
//全选与取消全选
function CheckAllBox(box)    
{ 
    CheckAOrF = false;
    CFNList.length=0;
    var fmlist = FriendNoList.length;
    for(var i = 0; i < fmlist; i++)
    {
        $get(FriendNoList[i]).checked = box.checked;        
    }
}
//朋友校验（朋友号）
function CheckFriend(friendNo,box)
{
    var friendInfo = {no:friendNo,na:box.value}
    if(box.checked)
    {   
        if(CheckAOrF)
        {
            for(var i=0;i<CFNList.length;i++)
            {
                if(CFNList[i].no==friendNo)
                {
                    CFNList.splice(i,1);
                }
            }
        }else{CFNList.push(friendInfo);}
    }
    else
    {
        if($get("iv_checkDel").checked||CheckAOrF)
        {
            $get("iv_checkDel").checked=false; 
            CheckAOrF=true;
            CFNList.push(friendInfo);
        }
        else
        {
            for(var i=0;i<CFNList.length;i++)
            {
                if(CFNList[i].no==friendNo)
                {
                    CFNList.splice(i,1);
                }
            }
        }   
    }     
}
function inviteEmpty()
{
    $EndMessageBox$();
}
//发送邮件
function SendEmail()
{ 
    var emails =  $get("iv_txtEmail").value;
    if(emails=="")
    {
        $get("iv_emOthErr").style.display="";
        return;
    }
    emails = emails.replace(/，/gi,",").toString();
    var mails = emails.replace(/,/gi,"<br/>").toString();
    $get("iv_emOthErr").style.display="none";    
    $get("iv_SendText").innerHTML=mails;
    $get("Send").style.display="none";
    $get("SendContent").style.display="";    
    WebServices.WWW.Groups.SendMail(MyNickN,emails,GroupURL,ivGroupTitle);
    $get("iv_txtEmail").value="";   
}
//发送italki 邮件
function SendItalkiMail()
{
    if((CFNList.length<=0&&!$get("iv_checkDel").checked&&!CheckAOrF)||(FriendPage.RecordCount==CFNList.length&&CheckAOrF))
    {
        $get("iv_error1ID").style.display="";        
        return;
    }    
	$get("iv_error1ID").style.display="none";	
	if(!CheckAOrF)
	{
	    var nickNames="";	    
	    for(var i=0; i<CFNList.length; i++)
	    {
            nickNames+=CFNList[i].na+"<br/>";
	    }
	    $get("iv_SendText").innerHTML = nickNames;
	    $get("Send").style.display="none";
	    $get("SendContent").style.display="";
	}
	var fMNArr = "";
	for(var i=0; i<CFNList.length; i++)
    {
        fMNArr+=CFNList[i].no;
    } 
    CheckAOrF=$get("iv_checkDel").checked?true:CheckAOrF;
    WebServices.WWW.Groups.SendItalkiMail(MyNickN,fMNArr,GroupURL,CheckAOrF,ivGroupTitle);
    $get("iv_checkDel").checked=false;
    CheckAllBox($get("iv_checkDel").checked);
}

//获取页面数据（页索引）
function GetPageFriend(pageIndex)
{
	FriendNoList.length = 0;
    PageIndex = pageIndex;
    showProgressBar(true);	
    InitDataFriend(PageIndex);
}
//初始数据
function onsuccFriend(result)
{
    RendFriendList(result[0],"iv_FriendsList");
    RendPageNavigator(result[1],"iv_divPage","GetPageFriend");   
    showProgressBar(false);
}
//获取数据（页索引）
function InitDataFriend(pageIndex)
{   
    WebServices.WWW.Groups.GetMemberFriends(MyMemberNo,pageIndex,PageSize,onsuccFriend);
}
//替换数据
function RendFriendList(list,divID)
{
    var domDiv =$get(divID);
    if(domDiv!=null)
    {    
        if(Sys.UI.DomElement.containsCssClass(domDiv, "DisplayNone"))Sys.UI.DomElement.removeCssClass(domDiv, "DisplayNone");         
        if($get("iv_FriendsTemple")==null) return;        
        var item= $get("iv_FriendsTemple").innerHTML;
        var listBuilder = new Sys.StringBuilder();
        var listCount = list.length;
        for (var index = 0; index < listCount; index++) 
        {
            var obj = list[index];                     
            var tmp = item.replace(/\$MemberNo\$/gi,obj.MemberNo).replace(/\$NickName\$/gi,obj.Nickname);            
            listBuilder.append(tmp); 
            FriendNoList[index] = obj.MemberNo;           
        }
        var sTemp = listBuilder.toString();domDiv.innerHTML = sTemp;
        for(var i = 0; i < listCount; i++)
        {
            var obj = list[i];
            if($get("iv_checkDel").checked){$get(obj.MemberNo).checked = $get("iv_checkDel").checked;}
            else
            {
                if(CFNList.length!=0)$get(obj.MemberNo).checked=true;
                for(var j = 0; j < CFNList.length; j++)
                {
                    if(CFNList[j].no!=obj.MemberNo&&!CheckAOrF){$get(obj.MemberNo).checked=false;}
                    else if(CFNList[j].no==obj.MemberNo&&CheckAOrF){$get(obj.MemberNo).checked=false;}                    
                }
            }                        
        }                   
    }
}